#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    map<string, vector<int> > mp;
    int N, K;
    cin >> N >> K;
    int course, n;
    string s;

    for (int i = 0; i < K; ++i)
    {
        cin >> course >> n;
        for (int j = 0; j < n; ++j)
        {
            cin >> s;
            mp[s].push_back(course);
        }
    }

    vector<string> vs(N);
    for (int i = 0; i < N; ++i)
    {
        cin >> vs[i];
    }
    for (int i = 0; i < N; ++i)
    {
        cout << vs[i] << ' ' << mp[vs[i]].size();
        sort(mp[vs[i]].begin(), mp[vs[i]].end(), greater<int>());
        for (int j = 0; j < mp[vs[i]].size(); ++j)
        {
            cout << ' ' << mp[vs[i]][mp[vs[i]].size() - 1 - j];
        }
        cout << endl;
    }
}
